clear;
close all;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% VARIANCE DECOMPOSITION #1                                    %%%
%%% METHODOLOGY BY DI GIOVANNI, LEVCHENKO AND MEJEAN (ECMA 2014) %%%
%%%                                                              %%%
%%% VARIANCE CALCULATIONS - MASTER FILE                          %%%
%%%                                                              %%%     
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Program: this file calculates the aggregate variance decomposition for
% the whole ("all") sample.
%
%   I.   Create aggregate variance and components using time-varying weights
%        - Weights are "w_fnt"
%   II.  Create aggregate variance and components using set of constant weights
%        - Weights are "w_fn\tau" for \tau varying from 1995 - 2011
%   III. Decompose (constant weight) variance and components into the DIRECT
%        and LINK terms
%   IV.  Compute Herfindahl indices based on the distribution of weights
% 
% References to the following functions: 
%   - ag_growth.m (aggregate growth and its variance, based on time-varying 
%     weights)
%   - var_growth.m (variance of aggregate growth, based on constant
%     weights)
%   - var_only_growth.m (variance of aggregate growth, ignoring "within" 
%     components, i.e. covariance terms)
%   - herfindahl.m (Herfindahl index)

clear;
close all;

% Get data - convert “growth_regs_revctry_all.csv” to “reg_output_all_ca.mat” first
cd (‘/yourdirectory/’)
load('reg_output_all_ca');

gr_macrosec = gr_macro+gr_sec;

% TIME-VARYING WEIGHTS
% Create aggregate growth rates and components and compute standard
% deviations
wgr_variable = w_fp.*gr_variable;
[agr_variable sd_agvariable] = ag_growth(id_fp,year,wgr_variable);
wgr_idio = w_fp.*gr_idio;
[agr_idio sd_agidio] = ag_growth(id_fp,year,wgr_idio);
wgr_macrosec = w_fp.*gr_macrosec;
[agr_macrosec sd_agmacrosec] = ag_growth(id_fp,year,wgr_macrosec);

% SET OF CONSTANT ("TAU") WEIGHTS
% Create aggregate variance components: set of constant weights

[VAR_variable var_variable sd_variable] = var_growth(id_fp,year,gr_variable,w_fp);
[VAR_idio var_idio sd_idio] = var_growth(id_fp,year,gr_idio,w_fp);
[VAR_idio_dest var_idio_dest sd_idio_dest] = var_growth(id_fp,year,gr_idio_dest,w_fp);
[VAR_idio_com var_idio_com sd_idio_com] = var_growth(id_fp,year,gr_idio_com,w_fp);
[VAR_macrosec var_macrosec sd_macrosec] = var_growth(id_fp,year,gr_macrosec,w_fp);

% Decompose variance and components into the DIRECT and LINK terms
% VAR only: ignore covariances of "within" variance components

% Trick is to use dot products in calculating sums.

[var_only_variable sd_only_variable] = var_only_growth(id_fp,year,gr_variable,w_fp);
[var_only_idio sd_only_idio] = var_only_growth(id_fp,year,gr_idio,w_fp);
[var_only_idio_dest sd_only_idio_dest] = var_only_growth(id_fp,year,gr_idio_dest,w_fp);
[var_only_idio_com sd_only_idio_com] = var_only_growth(id_fp,year,gr_idio_com,w_fp);
[var_only_macrosec sd_only_macrosec] = var_only_growth(id_fp,year,gr_macrosec,w_fp);
var_direct = var_only_idio;
var_link = var_idio-var_only_idio;
sd_direct = sqrt(var_direct);
sd_link = sqrt(var_link);

% Compute Herfindahl indices across different levels of aggregation

herf_fp = herfindahl(id_fp,year,w_fp);
herf_f = herfindahl(id_f,year,w_f);
herf_ip = herfindahl(id_ip,year,w_ip);
herf_i = herfindahl(id_i,year,w_i);
herf_p = herfindahl(id_p,year,w_p);

t = linspace(min(year),max(year),(max(year)-min(year)+1))';

% HOUSEHOLD MECHANICS
% Save output to smaller file

clear data gr_* w_* year id*;
save output/master_variance_all_ca;

% Results in Appendix B

% Standard deviations - Levels and relative contributions - Table A4
actual=mean(sd_variable)
firm=mean(sd_idio)
macrosec=mean(sd_macrosec)
sd_firm_rel = sd_idio./sd_variable
sd_macrosec_rel = sd_macrosec./sd_variable
firm_rel = mean(sd_firm_rel)
macrosec_rel = mean(sd_macrosec_rel)

% Standard deviations - Levels and relative contributions - Table A5
firm= mean(sd_idio)
direct = mean(sd_direct)
link = mean(sd_link)
sd_direct_rel = sd_direct./sd_idio
sd_link_rel = sd_link./sd_idio
direct_rel = mean(sd_direct_rel)
link_rel = mean(sd_link_rel)

% Mean growth rates
actual_mean=mean(agr_variable)
firm_mean=mean(agr_idio)
macrosec_mean=mean(agr_macrosec)

% Actual standard deviations of aggregates
true_actual=std(agr_variable)
true_firm=std(agr_idio)
true_macrosec=std(agr_macrosec)
